#########################################################################
## Replication file for                                           
## "Structuring intra-party politics: a mixed-method study of ideological 
##    and hierarchical factions in parties"                 
##                                                    
## Authors: Ann-Kristin Kölln (ann-kristin.kolln@gu.se) and                                                  
##  		Jonathan Polk (jonathan.polk@svet.lu.se)
##              
## 28th June 2023
#########################################################################


# R version 4.3.0 (2023-04-21) -- "Already Tomorrow"; Platform: x86_64-apple-darwin20 (64-bit)


#install and load packages
install.package("ggplot2")
library(ggplot2)


### FACTIONS ALONG LEFT-RIGHT DIMENSION ###


# load data file for factions on the left-right scale
data2<-read.csv2("factions_LR.csv", header=TRUE, sep=";")

#rename parties
data2$partynames[data2$party == "C"]<-"Center Party"
data2$partynames[data2$party == "KD"]<-"Christian Democrats"
data2$partynames[data2$party == "L"]<-"Liberals"
data2$partynames[data2$party == "M"]<-"Moderates"
data2$partynames[data2$party == "Mp"]<-"Green Party"
data2$partynames[data2$party == "S"]<-"Social Democrats"
data2$partynames[data2$party == "V"]<-"Left Party"

data2 $partynames <-as.factor(data2 $partynames)

#change order of levels in party variable
data2$party2 <- reorder(data2$partynames, data2$general_position)

#change order of factions
data2 $faction<-as.factor(data2 $faction)
data2 $faction <- factor(data2 $faction, levels = c("left","centre", "right"))


# generate Figure 4
plot1 <- ggplot(data2, aes(x = cluster_position, y = party2, size = cluster_size, fill = faction)) + 
        geom_point(alpha = 0.7, shape = 21) +
        scale_size(breaks = c(5, 20, 40, 60), range = c(8,25), guide=guide_legend(title ="faction size, %")) +
        scale_x_continuous(breaks = seq(0,11,1), limits = c(0,10)) +
        labs(x = "Left-Right Scale", y = "") +
        geom_text(aes(label=sprintf('%.1f',  cluster_size)), size = 3) +
        guides(fill = guide_legend(override.aes = list(size = 10))) +
        scale_fill_grey() +
        theme_bw()

jpeg("Figure 4.jpeg", width= 8, height= 6, units='in', res=300)        
plot1 
dev.off()



### FACTIONS ALONG GAL-TAN DIMENSION ### 

# load data file for factions on the left-right scale
data3<-read.csv2("factions_GT.csv", header=TRUE, sep=";")

#rename parties
data3 $partynames[data3 $party == "C"]<-"Center Party"
data3 $partynames[data3 $party == "KD"]<-"Christian Democrats"
data3 $partynames[data3 $party == "L"]<-"Liberals"
data3 $partynames[data3 $party == "M"]<-"Moderates"
data3 $partynames[data3 $party == "Mp"]<-"Green Party"
data3 $partynames[data3 $party == "S"]<-"Social Democrats"
data3 $partynames[data3 $party == "V"]<-"Left Party"

data3 $partynames <-as.factor(data3 $partynames)

#change order of levels in party variable
data3$party2 <- reorder(data3$partynames, data3$general_position)

#change order of factions
data3 $faction<-as.factor(data3 $faction)
data3 $faction <- factor(data3 $faction, levels = c("GAL","centre", "TAN"))


# generating Figure 5
plot2 <- ggplot(data3, aes(x = cluster_position, y = party2, size = cluster_size, fill = faction)) + 
        geom_point(alpha = 0.7, shape = 21) +
        scale_size(breaks = c(20, 40, 60), range = c(8,25), guide=guide_legend(title ="faction size, %")) +
        scale_x_continuous(breaks = seq(0,11,1), limits = c(0,10)) +
        labs(x = "GAL-TAN Scale", y = "") +
        geom_text(aes(label=sprintf('%.1f',  cluster_size)), size = 3) +
        guides(fill = guide_legend(override.aes = list(size = 10))) +
        scale_fill_grey() +
        theme_bw()
        

jpeg("Figure 5.jpeg", width= 8, height= 6, units='in', res=300)        
plot2 
dev.off()
